package com.jun.seckill_by_jun.service;

import com.alibaba.druid.util.StringUtils;
import com.jun.seckill_by_jun.dao.SeckillUserDao;
import com.jun.seckill_by_jun.domain.SeckillUser;
import com.jun.seckill_by_jun.exception.GlobalException;
import com.jun.seckill_by_jun.redis.RedisService;
import com.jun.seckill_by_jun.redis.SeckillUserKey;
import com.jun.seckill_by_jun.result.CodeMsg;
import com.jun.seckill_by_jun.util.MD5Util;
import com.jun.seckill_by_jun.util.UUIDUtil;
import com.jun.seckill_by_jun.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Service
public class SeckillUserService {
    @Autowired
    private SeckillUserDao seckillUserDao;
    @Autowired
    private RedisService redisService;

    public static final String COOKI_NAME_TOKEN = "token";

    public SeckillUser getById(Long id){
        SeckillUser user = redisService.get(SeckillUserKey.getById, "" + id, SeckillUser.class);
        if(user != null){
            return user;
        }
        user = seckillUserDao.getById(id);
        if(user != null){
            redisService.set(SeckillUserKey.getById,""+id,user);
        }
        return user;
    }

    public boolean updatePassword(String token, long id,String formPass){
        //取user
        SeckillUser user = getById(id);
        if(user == null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //更新数据库
        SeckillUser toBeUpdate = new SeckillUser();
        toBeUpdate.setId(id);
        toBeUpdate.setPassword(MD5Util.formPassToDBPass(formPass,user.getSalt()));
        seckillUserDao.update(toBeUpdate);
        //更新缓存:先删除再插入
        redisService.delete(SeckillUserKey.getById,""+id);
        user.setPassword(toBeUpdate.getPassword());
        redisService.set(SeckillUserKey.token,token,user);
        return true;
    }

    public String login(HttpServletResponse response ,LoginVo loginVo) {
        if(loginVo == null){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile = loginVo.getMobile();
        String formPass = loginVo.getPassword();
        //判断手机号;
        SeckillUser user = getById(Long.parseLong(mobile));
        if(user == null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        String dbPass = user.getPassword();
        String dbSalt = user.getSalt();
        String calcPass = MD5Util.formPassToDBPass(formPass, dbSalt);
        if(!calcPass.equals(dbPass)){
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        String token = UUIDUtil.uuid();
        addCookie(response,token,user);
        return token;
    }
    private void addCookie(HttpServletResponse response,String token,SeckillUser user){
        redisService.set(SeckillUserKey.token,token,user);
        Cookie cookie = new Cookie(COOKI_NAME_TOKEN,token);
        cookie.setMaxAge(SeckillUserKey.token.expireSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }
    public SeckillUser getUserByToken(HttpServletResponse response,String token){
        if(StringUtils.isEmpty(token)){
            return null;
        }
        SeckillUser user = redisService.get(SeckillUserKey.token, token, SeckillUser.class);
        if(user != null){
            addCookie(response,token,user);
        }
        return user;
    }
}
